#!/usr/bin/env bash

# ======== 
# 设置 OneX 项目源码根目录。因为 OneX 在安装部署时要引用很多仓库中的脚本
# 文件等，为了能够更准确的引用文件，减少出错概率和混乱，安装脚本引用的都是绝对路径
# 绝对路径的起始位置便是 ONEX_ROOT
# 注意：因为实际操作时，通常不会直接修改 `manifests/env.local` 源文件，而是将
# `manifests/env.local` 文件拷贝一份到到其他位置，然后修改文件中环境变量的值
# 所以，这种情况下，不能直接通过环境变量文件，来获取 OneX 的项目根目录路径
# 这时候，支持直接设置 ONEX_ROOT 环境变量来指定 OneX 项目根目录
# 如果不设置 ONEX_ROOT 环境变量，则会使用默认的 DEFAULT_ONEX_ROOT
export DEFAULT_ONEX_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
export ONEX_ROOT=${ONEX_ROOT:-${DEFAULT_ONEX_ROOT}} # OneX 项目源码根目录

# OneX 组件的统一访问 IP 地址，通常需要配置为安装机器的 IP 地址
export ONEX_ACCESS_HOST=${ONEX_ACCESS_HOST:-127.0.0.1}
# 为了防止在安装时跟本地已有组件起端口冲突，常用的第三方组件端口可以加一个端口号前缀
# 虽然，这稍微增加了部署的复杂度，但为了能让部署成功率更高，就加了这么一个端口号前缀
# 有些不常用的组件不需要加这个端口号前缀
# 如果你需要设置端口号前缀，3 通常是一个可用的值
export ONEX_ACCESS_PORT_PREFIX=${ONEX_ACCESS_PORT_PREFIX:-""}
export ONEX_IMAGE_VERSION=${ONEX_IMAGE_VERSION:-v0.1.0}

# ！注意：ONEX_LOG_OUTPUT 当前值要么为空，要么为 stdout
export ONEX_LOG_OUTPUT=stdout

# 生成文件存放目录
export LOCAL_OUTPUT_ROOT=${ONEX_ROOT}/_output

# 设置 OneX 项目统一的用户名/密码，方便记忆
export ONEX_USERNAME=onex
export ONEX_PASSWORD='onex(#)666'

# Linux系统 going 用户
export LINUX_USERNAME=going
# Linux root & going 用户密码
export LINUX_PASSWORD=${ONEX_PASSWORD}

# OneX 通用配置
export ONEX_INSTALL_DIR=/opt/onex # onex 安装文件存放目录（基础目录）
export ONEX_DATA_DIR=${ONEX_INSTALL_DIR}/data # onex 各组件数据目录
export ONEX_BIN_DIR=${ONEX_INSTALL_DIR}/bin # onex 各组件二进制文件存放目录
export ONEX_CONFIG_DIR=${ONEX_INSTALL_DIR}/etc # onex 配置文件存放目录
export ONEX_LOG_DIR=${ONEX_INSTALL_DIR}/log # onex 日志文件存放目录
export ONEX_CA_FILE=${ONEX_CONFIG_DIR}/cert/ca.pem # CA
export ONEX_THIRDPARTY_INSTALL_DIR=/data/onex.thirdparty # onex 依赖存储组件数据存放目录

# MariaDB 配置信息
export ONEX_MYSQL_HOST=${ONEX_MYSQL_HOST:-${ONEX_ACCESS_HOST}} # MariaDB 主机地址
export ONEX_MYSQL_PORT=${ONEX_ACCESS_PORT_PREFIX}3306
export ONEX_MYSQL_ADDR=${ONEX_MYSQL_HOST}:${ONEX_MYSQL_PORT}
export ONEX_MYSQL_ADMIN_USERNAME=root # MariaDB root 用户
export ONEX_MYSQL_ADMIN_PASSWORD=${ONEX_PASSWORD} # MariaDB root 用户密码
export ONEX_MYSQL_DATABASE=onex # MariaDB onex 应用使用的数据库名
export ONEX_MYSQL_USERNAME=${ONEX_USERNAME} # onex 数据库用户名
export ONEX_MYSQL_PASSWORD=${ONEX_PASSWORD} # onex 数据库用户名
export ONEX_MYSQL_LOG_LEVEL=1 # 数据库日志级别，1 为最低，4 为最高

# Redis 配置信息
export ONEX_REDIS_HOST=${ONEX_REDIS_HOST:-${ONEX_ACCESS_HOST}} # Redis 主机地址
export ONEX_REDIS_PORT=${ONEX_ACCESS_PORT_PREFIX}6379
export ONEX_REDIS_ADDR=${ONEX_REDIS_HOST}:${ONEX_REDIS_PORT}
export ONEX_REDIS_DATABASE=0 # Redis 主机地址
export ONEX_REDIS_USERNAME=${ONEX_USERNAME} # Redis 用户名
export ONEX_REDIS_PASSWORD=${ONEX_PASSWORD} # Redis 密码

# Etcd 配置信息
export ONEX_ETCD_HOST=${ONEX_ETCD_HOST:-${ONEX_ACCESS_HOST}}
export ONEX_ETCD_CLIENT_PORT=${ONEX_ACCESS_PORT_PREFIX}2379
export ONEX_ETCD_PEER_PORT=${ONEX_ACCESS_PORT_PREFIX}2380
# Etcd 端点列表
export ONEX_ETCD_ENDPOINTS=${ONEX_ETCD_HOST}:${ONEX_ETCD_CLIENT_PORT}

# MongoDB 配置
export ONEX_MONGO_HOST=${ONEX_MONGO_HOST:-${ONEX_ACCESS_HOST}} # MongoDB 地址
export ONEX_MONGO_PORT=27017 # MongoDB 端口
export ONEX_MONGO_URL=${ONEX_MONGO_HOST}:${ONEX_MONGO_PORT} # MongoDB URL
export ONEX_MONGO_ADMIN_USERNAME=root # MongoDB root 用户
export ONEX_MONGO_ADMIN_PASSWORD=${ONEX_PASSWORD} # MongoDB root 用户密码
export ONEX_MONGO_DATABASE=onex # MongoDB 用户名
export ONEX_MONGO_COLLECTION=audit # MongoDB 密码
export ONEX_MONGO_USERNAME=${ONEX_USERNAME} # MongoDB 用户名
export ONEX_MONGO_PASSWORD=${ONEX_PASSWORD} # MongoDB 密码
export ONEX_MONGO_TIMEOUT=30s # MongoDB 密码

# Kafka 配置
export ONEX_KAFKA_HOST=${ONEX_KAFKA_HOST:-${ONEX_ACCESS_HOST}}
export ONEX_KAFKA_PORT=${ONEX_ACCESS_PORT_PREFIX}9092
export ONEX_KAFKA_BROKERS=${ONEX_KAFKA_HOST}:${ONEX_KAFKA_PORT}
export ONEX_KAFKA_TOPIC=audit
export ONEX_KAFKA_TIMEOUT=3s

# Jaeger 配置
export ONEX_JAEGER_HOST=${ONEX_JAEGER_HOST:-${ONEX_ACCESS_HOST}}
export ONEX_JAEGER_PORT=${ONEX_ACCESS_PORT_PREFIX}4317
export ONEX_JAEGER_ENV=dev
export ONEX_JAEGER_ENDPOINT=${ONEX_JAEGER_HOST}:${ONEX_JAEGER_PORT}

# Kubeconfig 路径配置
export ONEX_ADMIN_KUBECONFIG=${ONEX_CONFIG_DIR}/config

# OneX 服务配置
#
## onex-fakeserver 配置
export ONEX_FAKESERVER_HTTP_PORT=58843
export ONEX_FAKESERVER_GRPC_PORT=58090
# 为了简化安装，所有组件的绑定地址为 0.0.0.0
export ONEX_FAKESERVER_HTTP_ADDR=0.0.0.0:${ONEX_FAKESERVER_HTTP_PORT}
export ONEX_FAKESERVER_GRPC_ADDR=0.0.0.0:${ONEX_FAKESERVER_GRPC_PORT}
export ONEX_FAKESERVER_TLS_USE_TLS=false
export ONEX_FAKESERVER_TLS_CERT=${ONEX_CONFIG_DIR}/cert/onex-fakeserver.pem
export ONEX_FAKESERVER_TLS_KEY=${ONEX_CONFIG_DIR}/cert/onex-fakeserver-key.pem
export ONEX_FAKESERVER_LOG_OUTPUT=${ONEX_LOG_OUTPUT:-${ONEX_LOG_DIR}/onex-fakeserver.log}

## onex-usercenter 配置
export ONEX_USERCENTER_HOST=${ONEX_USERCENTER_HOST:-${ONEX_ACCESS_HOST}}
export ONEX_USERCENTER_HTTP_PORT=50843
export ONEX_USERCENTER_GRPC_PORT=50090
export ONEX_USERCENTER_HTTP_ADDR=0.0.0.0:${ONEX_USERCENTER_HTTP_PORT}
export ONEX_USERCENTER_GRPC_ADDR=0.0.0.0:${ONEX_USERCENTER_GRPC_PORT}
export ONEX_USERCENTER_TLS_USE_TLS=false
export ONEX_USERCENTER_TLS_CERT=${ONEX_CONFIG_DIR}/cert/onex-usercenter.pem
export ONEX_USERCENTER_TLS_KEY=${ONEX_CONFIG_DIR}/cert/onex-usercenter-key.pem
export ONEX_USERCENTER_REDIS_DATABASE=${ONEX_REDIS_DATABASE}
export ONEX_USERCENTER_LOG_OUTPUT=${ONEX_LOG_OUTPUT:-${ONEX_LOG_DIR}/onex-usercenter.log}

## onex-apiserver
export ONEX_APISERVER_SECURE_PORT=52443
export ONEX_APISERVER_HOST=${ONEX_APISERVER_HOST:-${ONEX_ACCESS_HOST}}
export ONEX_APISERVER_BIND_ADDRESS=0.0.0.0
export ONEX_APISERVER_CLIENT_CA_FILE=${ONEX_CA_FILE}
export ONEX_APISERVER_TLS_CERT_FILE=${ONEX_CONFIG_DIR}/cert/onex-apiserver.pem
export ONEX_APISERVER_TLS_PRIVATE_KEY_FILE=${ONEX_CONFIG_DIR}/cert/onex-apiserver-key.pem
export ONEX_APISERVER_ETCD_SERVERS=${ONEX_ETCD_ENDPOINTS}
export ONEX_APISERVER_V_LEVEL=7

## onex-gateway 配置
export ONEX_GATEWAY_HOST=${ONEX_GATEWAY_HOST:-${ONEX_ACCESS_HOST}}
export ONEX_GATEWAY_HTTP_PORT=51843
export ONEX_GATEWAY_GRPC_PORT=51090
export ONEX_GATEWAY_HTTP_ADDR=0.0.0.0:${ONEX_GATEWAY_HTTP_PORT}
export ONEX_GATEWAY_GRPC_ADDR=0.0.0.0:${ONEX_GATEWAY_GRPC_PORT}
export ONEX_GATEWAY_TLS_USE_TLS=false
export ONEX_GATEWAY_TLS_CERT=${ONEX_CONFIG_DIR}/cert/onex-usercenter.pem
export ONEX_GATEWAY_TLS_KEY=${ONEX_CONFIG_DIR}/cert/onex-usercenter-key.pem
export ONEX_GATEWAY_REDIS_DATABASE=${ONEX_REDIS_DATABASE}
export ONEX_GATEWAY_USERCENTER_SERVER=${ONEX_GATEWAY_USERCENTER_SERVER:-${ONEX_USERCENTER_HOST}:${ONEX_USERCENTER_GRPC_PORT}}
export ONEX_GATEWAY_LOG_OUTPUT=${ONEX_LOG_OUTPUT:-${ONEX_LOG_DIR}/onex-gateway.log}

# onex-nightwatch
export ONEX_NIGHTWATCH_HEALTH_CHECK_PORT=54082
export ONEX_NIGHTWATCH_HEALTH_ENABLE_HTTP_PROFILE=true
export ONEX_NIGHTWATCH_HEALTH_CHECK_PATH=/healthz
export ONEX_NIGHTWATCH_HEALTH_CHECK_ADDRESS=0.0.0.0:${ONEX_NIGHTWATCH_HEALTH_CHECK_PORT}
export ONEX_NIGHTWATCH_REDIS_DATABASE=${ONEX_REDIS_DATABASE}
export ONEX_NIGHTWATCH_LOG_OUTPUT=${ONEX_LOG_OUTPUT:-${ONEX_LOG_DIR}/onex-nightwatch.log}

## onex-pump 配置
export ONEX_PUMP_HEALTH_CHECK_PORT=55082
export ONEX_PUMP_HEALTH_ENABLE_HTTP_PROFILER=true
export ONEX_PUMP_HEALTH_CHECK_PATH=/healthz
export ONEX_PUMP_HEALTH_CHECK_ADDRESS=0.0.0.0:${ONEX_PUMP_HEALTH_CHECK_PORT}
export ONEX_PUMP_REDIS_DATABASE=${ONEX_REDIS_DATABASE}
export ONEX_PUMP_LOG_OUTPUT=${ONEX_LOG_OUTPUT:-${ONEX_LOG_DIR}/onex-pump.log}

## onex-controller-mananger
export ONEX_CONTROLLER_MANAGER_METRICS_PORT=${ONEX_CONTROLLER_MANAGER_METRICS_PORT:-10249}
export ONEX_CONTROLLER_MANAGER_HEALTHZ_PORT=${ONEX_CONTROLLER_MANAGER_HEALTHZ_PORT:-10256}

## onex-miner-controller
export ONEX_MINER_CONTROLLER_REDIS_DATABASE=${ONEX_REDIS_DATABASE}
export ONEX_MINER_CONTROLLER_METRICS_PORT=${ONEX_MINER_CONTROLLER_METRICS_PORT:-20251}
export ONEX_MINER_CONTROLLER_HEALTHZ_PORT=${ONEX_MINER_CONTROLLER_HEALTHZ_PORT:-20250}

## onex-minerset-controller
export ONEX_MINERSET_CONTROLLER_METRICS_PORT=${ONEX_MINERSET_CONTROLLER_METRICS_PORT:-20249}
export ONEX_MINERSET_CONTROLLER_HEALTHZ_PORT=${ONEX_MINERSET_CONTROLLER_HEALTHZ_PORT:-20250}

## onex-toyblc
export ONEX_TOYBLC_ADDRESS=0x210d9eD12CEA87E33a98AA7Bcb4359eABA9e800e
export ONEX_TOYBLC_USERNAME=${ONEX_USERNAME}
export ONEX_TOYBLC_PASSWORD=${ONEX_PASSWORD}
export ONEX_TOYBLC_HTTP_PORT=56080
export ONEX_TOYBLC_MINER=false
export ONEX_TOYBLC_P2P_ADDR=0.0.0.0:6001
export ONEX_TOYBLC_PEERS=ws://localhost:6001
export ONEX_TOYBLC_HTTP_ADDR=0.0.0.0:${ONEX_TOYBLC_HTTP_PORT}

# onex-cacheserver
export ONEX_CACHESERVER_GRPC_PORT=57090
export ONEX_CACHESERVER_DISABLE=true
export ONEX_CACHESERVER_GRPC_ADDR=0.0.0.0:${ONEX_CACHESERVER_GRPC_PORT}
export ONEX_CACHESERVER_TLS_USE_TLS=false
export ONEX_CACHESERVER_TLS_CERT=${ONEX_CONFIG_DIR}/cert/onex-cacheserver.pem
export ONEX_CACHESERVER_TLS_KEY=${ONEX_CONFIG_DIR}/cert/onex-cacheserver-key.pem
export ONEX_CACHESERVER_REDIS_DATABASE=${ONEX_REDIS_DATABASE}
export ONEX_CACHESERVER_LOG_OUTPUT=${ONEX_LOG_OUTPUT:-${ONEX_LOG_DIR}/onex-cacheserver.log}

# onexctl
export ONEXCTL_USER_USERNAME=admin
export ONEXCTL_USER_PASSWORD=${ONEX_PASSWORD}
export ONEXCTL_USERCENTER_ADDR=${ONEXCTL_USERCENTER_ADDR:-${ONEX_USERCENTER_HOST}:${ONEX_USERCENTER_HTTP_PORT}}
export ONEXCTL_GATEWAY_ADDR=${ONEXCTL_GATEWAY_ADDR:-${ONEX_GATEWAY_HOST}:${ONEX_GATEWAY_HTTP_PORT}}
